﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using DapperExtensions;
using DapperWrapper;
using ZL.CRM.CommonEntity.AccOpr.Web;
using ZLWeb.WebApiEntity;
using ZLWeb.WebApiEntity.WechatCardModel;
using ZLWeb.WebApiEntityBase;
using ZLWeb.WebApiRepository.InterFace;
using ZilLion.Service.Core.CommonRepository;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;

namespace ZLWeb.WebApiRepository
{
    public class BindOldCardRepository : CRMSqlServerRespository<WebApiEntity.WechatCardModel.AccBindingCardYwdetail>, IBindOldCardRepository
    {
        public BindOldCardRepository(long? Accid) : base(Accid)
        {
        }

        public BindOldCardRepository(IDbContext context, long? Accid) :base(context)
        {
            this.Accid = Accid.GetValueOrDefault();
        }

        public ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno BindOldCard(ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardYwdetail info)
        {
               DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "accid", Val: Accid));
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            var ac = ExecuteProcAddParam(info.Ywcore, "#ywcore", true);
            paActions.Add(ac);
            ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno memberno = new ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno();
            var ywInfoparam = ExecuteProcAddParam(memberno, "#memberno", false);
            paActions.Add(ywInfoparam);
            ExecuteProc(paActions, "pr_AccBindingCard_submit_new", p, (con) =>
            {
                memberno = con.GetList<ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno>().FirstOrDefault();
            });
            return ReturnResponseResultInfo<ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno>(p) ?? memberno?? new ZL.CRM.CommonEntity.AccOpr.Web.AccBindingCardMemberno();
        }
    }
}